import React, { Component, Fragment } from 'react';
import { Link } from 'dva/router';
import { connect } from 'dva';
import TagRule from './tagRule'
import router from 'umi/router';
import styles from './index.less';
import { Card, Table, Divider, Radio, Button, Row, Col, Form, Icon, Tree, Spin, message } from 'antd';
import Input from '@/components/Input/index'
const RadioGroup = Radio.Group;
const FormItem = Form.Item;
const namespace = 'customertag'
@Form.create()
class EditTags extends React.Component {

    componentWillMount() {
      window.Isoption = false;
        this.findTag();

    }
    componentDidMount() {
      window.Isoption = true;
    }

    findTag = () => {
        let { dispatch, match } = this.props;
        let tagId = match.params.id;
        dispatch({
            type: `${namespace}/FindTag`,
            param: { id: tagId,p: Math.random()},
        });
    }

    handleSubmit = (e) => {
        let { dispatch, match } = this.props;

        this.props.form.validateFields((err, values) => {
            if (!err) {
                values = { ...values, id: parseInt(match.params.id) }
                console.log(values)
                dispatch({
                    type: `${namespace}/UpdateTag`,
                    param: { ...values, p: Math.random() },
                    callback: (data) => {
                        if (data.Code == 0) {
                            message.success('修改成功')
                            router.replace({
                                pathname: `/${window.storeId}/customer/tags`,
                            });
                        } else {
                            message.error('添加失败')
                        }
                    }

                });

            }
        });
    }
    render() {
        const formItemLayout = {
            labelCol: {
                xs: { span: 6 },
                sm: { span: 6 },
                md: { span: 6 },

            },
            wrapperCol: {
                xs: { span: 8 },
                sm: { span: 8 },
                md: { span: 8 },
            },
        };
        const { customertag: { TagModel }, loading, loadbtn } = this.props;
        const {
            getFieldDecorator, getFieldsError, getFieldError, isFieldTouched, getFieldValue
        } = this.props.form;
        return (
            <Spin spinning={loading} >
                <Card bordered={false} className={styles.AddTags}>
                    <Form>

                        <FormItem
                            {...formItemLayout}
                            //validateStatus={roleNameError ? 'error' : ''}

                            label="标签名称:"
                        >
                            {getFieldDecorator('Name', {
                                initialValue: TagModel && TagModel.Name,
                                rules: [{
                                    required: true, message: '标签名称必须填写',
                                }],
                            })(
                                <Input maxLength={6} showtotal />
                            )}
                        </FormItem>


                        <FormItem
                            {...formItemLayout}
                            label="标签类型"
                        >

                            {getFieldDecorator('tagType', {
                                initialValue: TagModel && TagModel.TagType,
                                rules: [{ required: true, message: '请选择标签类型!' }],
                            })(
                                <RadioGroup disabled={true}>
                                    <Radio value={1}>手动标签</Radio>
                                    <Radio value={2}>自动标签</Radio>
                                </RadioGroup>
                            )}

                        </FormItem>
                        {getFieldValue("tagType") != 2 ? null :
                            <FormItem
                                // {...formItemLayout}
                                label=""
                            >
                                {getFieldDecorator('TagRule', {
                                    initialValue: TagModel && TagModel.TagRule,
                                    rules: [{ required: true, message: '请设置标签规则!' }],
                                })(
                                    <TagRule form={this.props.form} />
                                )}

                            </FormItem>
                        }
                        <Row>
                            <Col offset={6}>
                                <Button
                                    type="primary"

                                    loading={loadbtn}
                                    className={styles.SaveBtn}
                                    onClick={this.handleSubmit}
                                >
                                    保存
                            </Button>
                            </Col>
                        </Row>
                    </Form>
                </Card>
            </Spin>
        )
    }
}
export default connect(({ customertag, global, loading }) => ({
    customertag,
    loading: loading.effects['customertag/FindTag'],
    loadbtn: loading.effects['customertag/UpdateTag'],
}))(EditTags);
